package cn.sxt.yxl.users.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.yxl.common.util.PageInfoUtil;
import cn.sxt.yxl.users.dao.IAAdminsDao;
import cn.sxt.yxl.users.dao.IRoleDao;
import cn.sxt.yxl.users.pojo.AAdmins;
import cn.sxt.yxl.users.pojo.ARole;
import cn.sxt.yxl.users.service.IUsersService;

@Service("usersService")
public class UsersServiceImpl implements IUsersService{
	
	@Resource
	private IAAdminsDao adminsDao;
	
	@Resource
	private IRoleDao roleDao;
	
	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap) {
		
		return this.adminsDao.findOne(condMap);
	}

	@Override
	public JSONObject saveOneAdminsService(AAdmins admins) {
		JSONObject resultJSON = new JSONObject();
		/**保存记录*/
		int res = this.adminsDao.saveOne(admins);
		
		if(res>0){
			/**存储结果*/
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId()+"");
			dataJSON.put("effectCount", res);
			
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
			
		}
		
		return resultJSON;
	}

	@Override
	public JSONObject updateOneAdminsService(AAdmins admins) {
		JSONObject resultJSON = new JSONObject();
		/**保存记录*/
		int res = this.adminsDao.updateOne(admins);
		
		if(res>0){
			/**存储结果*/
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId()+"");
			dataJSON.put("effectCount", res);
			
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
			
		}
		
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneAdminsService(AAdmins admins) {
		JSONObject resultJSON = new JSONObject();
		/**保存记录*/
		int res = this.adminsDao.deleteOne(admins);
		
		if(res>0){
			/**存储结果*/
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId()+"");
			dataJSON.put("effectCount", res);
			
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
			
		}
		
		return resultJSON;
		
	}

	@Override
	public List<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil !=null){
			/**分页*/
			/**查询总记录数*/
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			/**查询数据*/
			List<AAdmins> adminsList = this.adminsDao.findCondList(condMap);
			/**设置总记录数*/
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return adminsList;
		}
		return this.adminsDao.findCondList(condMap);
	}

	@Override
	public ARole findOneRoleService(Map<String, Object> condMap) {
		return this.roleDao.findOne(condMap);
	} 

	@Override
	public JSONObject saveOneRoleService(ARole role) {
		JSONObject resultJSON = new JSONObject();
		/**保存记录*/
		int res = this.roleDao.saveOne(role);
		
		if(res>0){
			/**存储结果*/
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", role.getId()+"");
			dataJSON.put("effectCount", res);
			
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
			
		}
		
		return resultJSON;
	}

	@Override
	public JSONObject updateOneRoleService(ARole role) {
		JSONObject resultJSON = new JSONObject();
		/**保存记录*/
		int res = this.roleDao.updateOne(role);
		
		if(res>0){
			/**存储结果*/
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", role.getId()+"");
			dataJSON.put("effectCount", res);
			
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
			
		}
		
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneRoleService(ARole role) {
		JSONObject resultJSON = new JSONObject();
		/**保存记录*/
		int res = this.roleDao.deleteOne(role);
		
		if(res>0){
			/**存储结果*/
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", role.getId()+"");
			dataJSON.put("effectCount", res);
			
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
			
		}
		
		return resultJSON;
		
	}

	@Override
	public List<ARole> findCondListRoleService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil !=null){
			/**分页*/
			/**查询总记录数*/
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			/**查询数据*/
			List<ARole> roleList = this.roleDao.findCondList(condMap);
			/**设置总记录数*/
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return roleList;
		}
		return this.roleDao.findCondList(condMap);
	}
	
	
}
